﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using IQKS.AWP.BusinessAccessLayer;
using IQKS.AWP.BusinessObject;
using System.Configuration;

public partial class Admin_BriefMaster : System.Web.UI.Page
{
    Guid gudUserID = CommonUtility.GetLoggedOnUserId();
    private DataSet GetDataSet()
    {

        DataSet objDs=null;
        try
        {
            objDs = new BriefMasterBAL().GetBriefMaster();
        }
        catch (Exception exception)
        {
            exception.AddErrorToLog(); //throw new ApplicationException(exception.Message);
        }
        return objDs;

    }
    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (!IsPostBack)
        {
            string userid = Convert.ToString(CommonUtility.GetLoggedOnUserId());
            GetCountryMaster();
            GetData();
            //Session["UserID"] = Guid.NewGuid();
            lblErrorMessage.Text="";
        }


    }


    private void GetCountryMaster()
    {
        ddlCountryName.DataSource = GetCountryMasterDataSet();
        ddlCountryName.DataTextField = "CountryName";
        ddlCountryName.DataValueField = "CountryID";
        ddlCountryName.DataBind();
        ddlCountryName.Items.Insert(0, new ListItem("-- Select Country Name --", "0"));
    }

    private DataSet GetCountryMasterDataSet()
    {

        DataSet objDs=null;
        try
        {
            objDs = new CountryMasterBAL().GetCountryMaster();
        }
        catch (Exception exception)
        {
            exception.AddErrorToLog();// throw new ApplicationException(exception.Message);
        }
        return objDs;

    }   
    protected void btnClear_Click(object sender, EventArgs e)
    {
        txtBriefTitle.Text = string.Empty;
        lblErrorMessage.Text = string.Empty;
        hdnInput.Value = string.Empty;
        //txtLanguageId.Text = "";
        GetData();
    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Admin/BriefMasterModify.aspx?bgid=0&mod=in");
   
    }

    protected void gvBrief_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton l = (LinkButton)e.Row.FindControl("lnkDelete");
            if (Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "IsActive")) == 0)
            {
                l.Attributes.Add("onclick", "javascript:return " + "confirm('Are you sure you want to active " + DataBinder.Eval(e.Row.DataItem, "BriefTitle") + " Brief?')");
            }
            else
            {
                l.Attributes.Add("onclick", "javascript:return " + "confirm('Are you sure you want to delete " + DataBinder.Eval(e.Row.DataItem, "BriefTitle") + " Brief?')");
            }


            LinkButton l2 = (LinkButton)e.Row.FindControl("lnkDeleteBriefAll");
            if (Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "IsActive")) == 1)
            {
                l2.Attributes.Add("onclick", "javascript:return " + "confirm('Are you sure you want to delete all candidates assigned for this Brief " + DataBinder.Eval(e.Row.DataItem, "BriefTitle") + " Brief?')");
            }
           
        }


    }
   
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        hdnInput.Value = "search";
        GetData();
    }
  
    protected void gvBrief_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvBrief.PageIndex = e.NewPageIndex;
        GetData();

    }
    protected void gvSearchResult_RowCommand(object sender, GridViewCommandEventArgs e)
    {
          
    }

    protected void gvBrief_RowCommand(object sender, GridViewCommandEventArgs e)
    {
         if (e.CommandName == "DeleteAll")
        {
            int intBriefID ;
            intBriefID = Convert.ToInt32(e.CommandArgument.ToString());


            DeleteBriefALL(intBriefID, gudUserID);
            GetData();
          
        }
        if (e.CommandName.ToUpper() == "POPUP")
        {
            //Eval("BriefID") + "=" + Eval("BriefTitle") + "=" + Eval("BriefDesc")
            string sBriefDetails = e.CommandArgument.ToString();
            if (sBriefDetails != "")
            {
                string[] splitname = sBriefDetails.Split('=');
                if (splitname.Length == 3)
                {
                    hfCntryBriefId.Value= splitname[0].ToString();
                    lblCntryBriefName.Text= splitname[1].ToString();
                    lblCntryBriefDesc.Text = splitname[1].ToString();
                }
            }
            DataSet ds = GetCountryMasterDataSet();
            CommonUtility.BindCheckBoxList(ref chkCountry, ds, "CountryName", "CountryID");
            mp1.Show();
        }

        //if (e.CommandName == "Edit")
        //{

        //    Response.Redirect("~/Admin/BriefMasterModify.aspx?bgid={0}&mod=up");
        //}

    }
   

    private void GetData()
    {
        if (hdnInput.Value == "search")
        {
            DataSet objDs = new DataSet();
            objDs = GetDataSet();
            DataView objDv = new DataView();
            objDv = objDs.Tables[0].DefaultView;
            if (txtBriefTitle.Text != string.Empty && ddlCountryName.SelectedIndex != 0)
                objDv.RowFilter = "(BriefTitle LIKE '*" + txtBriefTitle.Text.Trim() + "*') and (CountryID = " + ddlCountryName.SelectedValue + ")";
            else if (txtBriefTitle.Text != string.Empty && ddlCountryName.SelectedIndex == 0)
                objDv.RowFilter = "(BriefTitle LIKE '*" + txtBriefTitle.Text.Trim() + "*')";
            else if (ddlCountryName.SelectedIndex != 0)
                objDv.RowFilter = "(CountryID = " + ddlCountryName.SelectedValue + ")";
            gvBrief.DataSource = objDv;
        }
        else
        {
            gvBrief.DataSource = GetDataSet();
        }
        gvBrief.DataBind();
    }
    protected void ddlCountryName_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void gvBrief_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int intBriefID = (int)gvBrief.DataKeys[e.RowIndex].Value;
        string strBriefStatus = Convert.ToString(gvBrief.Rows[e.RowIndex].Cells[7].Text).ToUpper() == "ACTIVE" ? "DELETE" : "ACTIVE";
        DeleteBrief(intBriefID, strBriefStatus);
        GetData();

    }

    private void DeleteBriefALL(int intBriefID,Guid UserID)
    {
        BriefMasterBAL objBMBal;
            objBMBal = new BriefMasterBAL();
         string strReturnValue = (string)objBMBal.SetBriefMasterClearBrief(intBriefID,UserID);
    }
    protected void gvBrief_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    private void DeleteBrief(int intBrief, string strSetMethod)
    {
        BriefMasterBAL objBMBal;
        BriefMasterBO objBMBO;
        //Guid gudUserID = new Guid(Convert.ToString(Session["UserID"]));
        string strmod = Convert.ToString(Request.QueryString["mod"]);
        string strReturnValue;


        objBMBal = new BriefMasterBAL();
        objBMBO = new BriefMasterBO();

        objBMBO.SetMethod = strSetMethod;
        objBMBO.BriefID = intBrief;
        objBMBO.ModifyBy = gudUserID;
        objBMBO.ModifyDate = System.DateTime.Today;
        objBMBO.StBriefStartDate = System.DateTime.Today;
        objBMBO.StBriefEndDate = System.DateTime.Today;
        objBMBO.IsActive = (strSetMethod == "ACTIVE" ? true : false);
        strReturnValue = (string)objBMBal.SetBriefMaster(objBMBO);
        if (strReturnValue == "SUCCESS")
        {
            lblErrorMessage.Text = "Record has been updated succesfully.";
            lblErrorMessage.ForeColor = System.Drawing.Color.Green;
        }
        else
        {
            lblErrorMessage.Text = "Error while saving record.";
            lblErrorMessage.ForeColor = System.Drawing.Color.Red;
        }
    }
    public string GetBriefDownload(object objDocumentFile)
    {
        string strPathProDoc = string.Empty;
        strPathProDoc = Convert.ToString(ConfigurationSettings.AppSettings["BriefDoc"]) + Convert.ToString(objDocumentFile);
        strPathProDoc = "javascript:onClickLinkDocumentDownload('" + strPathProDoc.Replace("~", "..") + "')";
        return strPathProDoc;
    }
    public string GetGDDownload(object objDocumentFile)
    {
        string strPathProDoc = string.Empty;
        strPathProDoc = Convert.ToString(ConfigurationSettings.AppSettings["GDDoc"]) + Convert.ToString(objDocumentFile);
        strPathProDoc = "javascript:onClickLinkDocumentDownload('" + strPathProDoc.Replace("~", "..") + "')";
        return strPathProDoc;
    }
    protected void PopUpClose_Click(object sender, EventArgs e)
    {
        //int intstatusID = Convert.ToInt32(ddlCandidateStatus.SelectedValue);
        //GetDataAssigningAssessor(CommonUtility.GetLoggedOnUserId(), intstatusID);
        lblCountryErr.Text = "";
        mp1.Hide();
    }
    private int CountrySelected()
    {
        int intCount = 0;
        for (int i = 0; i < chkCountry.Items.Count; i++)
        {
            if (chkCountry.Items[i].Selected)
            {
                intCount = 1;
                return intCount;
            }
        }
        return intCount;
    }
    protected void cstCountry_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (CountrySelected() <= 0)
        {
            args.IsValid = false;
            return;
        }
    }

    protected void btnSaveToCountry_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {   
            string strReturnValue;
            string sCountryList = CountryList();
            Guid gUserId = CommonUtility.GetLoggedOnUserId();
            BriefMasterBAL objBMBal = new BriefMasterBAL();
            strReturnValue = (string)objBMBal.CopyBriefMasterBetweenCountry("COPY", sCountryList,hfCntryBriefId.Value, lblCntryBriefName.Text, gUserId);
            if (strReturnValue == "SUCCESS")
            {
                lblCountryErr.Text = "Record has been updated succesfully.";
                lblCountryErr.ForeColor = System.Drawing.Color.Green;
            }
            else
            {
                lblCountryErr.Text = "Error while saving record. :" + strReturnValue;
                lblCountryErr.ForeColor = System.Drawing.Color.Red;
            }
        }
        mp1.Show();
    }
    private string CountryList()
    {
        string strLanguageList = string.Empty;
        for (int i = 0; i < chkCountry.Items.Count; i++)
        {
            if (chkCountry.Items[i].Selected)
            {
                strLanguageList += chkCountry.Items[i].Value + ",";
            }
        }
        return strLanguageList.Substring(0, (strLanguageList.Length - 1));
    }
}